#include "debugcom.h"

unsigned char 	uarttxbuf[UART_TX_BUFF_SIZE];

uint8_t recv_set_buf[Recv_Set_Buf_Size_Max];
uint8_t send_set_buf[Send_Set_Buf_Size_Max];
unsigned short recv_set_len = 0;
void uart_send(unsigned char *buff, unsigned short len, unsigned char dmaflag)
{
  if(dmaflag == 1)
  {
    memset(uarttxbuf, 0, UART_TX_BUFF_SIZE);
    memcpy(uarttxbuf, buff, len);

    __HAL_UART_ENABLE_IT(&huart6,  UART_IT_TC);//TC interrupt
    HAL_UART_Transmit_DMA(&huart6, uarttxbuf, len);
  }
  else
  {
    HAL_UART_Transmit(&huart6, buff, len, 0xFF);
  }
}

// 电机的接收回调函数
void recv_set_parameter_package_callback( uint8_t *data, uint16_t size )
{
  if( size > Recv_Set_Buf_Size_Max )
  {
    return ;
  }
  memcpy(recv_set_buf, data, size);
  recv_set_len = size;
  rk.flag.recv_set_parameter = true;
}
void send_devaddr_to_uart( void )
{
  unsigned short datapos = 0;
  unsigned short sendlen = 0;
  unsigned short sumcrc  = 0;
  memset( send_set_buf, 0, Send_Set_Buf_Size_Max );
  send_set_buf[datapos]    = 0xAA;
  send_set_buf[datapos+1]  = 0x55;
  datapos+=2;
  send_set_buf[datapos]    = sendlen >> 8;
  send_set_buf[datapos+1]  = sendlen;
  datapos+=2;
  send_set_buf[datapos]  = 0x01;
  datapos+=1;
  memcpy( send_set_buf+datapos, &rk.hyper_parameter, 20 );
  datapos += 20;

  sendlen = datapos + 2;
  send_set_buf[2]    = sendlen >> 8;
  send_set_buf[3]  = sendlen;

  sumcrc = 0;
  for( int i = 0; i < sendlen - 2; i++ )
  {
      sumcrc += send_set_buf[i];
  }
  send_set_buf[sendlen - 2] = ( sumcrc >> 8 ) & 0xFF;
  send_set_buf[sendlen - 1] = ( sumcrc >> 0 ) & 0xFF;

  uart_send( send_set_buf, sendlen, 1 );
}

void send_set_addr_fail_to_uart( void )
{
  unsigned short datapos = 0;
  unsigned short sendlen = 0;
  unsigned short sumcrc  = 0;
  memset( send_set_buf, 0, Send_Set_Buf_Size_Max );
  send_set_buf[datapos]    = 0xAA;
  send_set_buf[datapos+1]  = 0x55;
  datapos+=2;
  send_set_buf[datapos]    = sendlen >> 8;
  send_set_buf[datapos+1]  = sendlen;
  datapos+=2;

  send_set_buf[datapos]  = 0x02;
  datapos+=1;

  sendlen = datapos + 2;
  send_set_buf[2]    = sendlen >> 8;
  send_set_buf[3]  = sendlen;

  sumcrc = 0;
  for( int i = 0; i < sendlen - 2; i++ )
  {
      sumcrc += send_set_buf[i];
  }
  send_set_buf[sendlen - 2] = ( sumcrc >> 8 ) & 0xFF;
  send_set_buf[sendlen - 1] = ( sumcrc >> 0 ) & 0xFF;

  uart_send( send_set_buf, sendlen, 1 );
}
uint8_t recv_uart_array_addr[10];
void set_devaddr( ADDR *addr )
{
  upload_package = upload_get_Whether_valid_addr_package;
  upload_package_init();
  upload_package_crc_end( upload_package() );
}
void recv_set_parameter_package_handle( void )
{
  unsigned short sumcrc  = 0;

  if(recv_set_buf[0] != 0xAA || recv_set_buf[1] != 0x55 )
  {
    return ;
  }

  for( int i = 0; i < recv_set_len - 2; i++ )
  {
      sumcrc += recv_set_buf[i];
  }
  if( (recv_set_buf[recv_set_len - 2] != (( sumcrc >> 8 ) & 0xFF)) || (recv_set_buf[recv_set_len - 1] != (sumcrc & 0xFF)) )
  {
    return ;
  }


  if( (recv_set_buf[2] << 8 | recv_set_buf[3]) == 0x08 )
  {
    if( recv_set_buf[4] != 0x12 || recv_set_buf[5] != 0x10 )
    {
      return ;
    }
    send_devaddr_to_uart();
  }
  else if( (recv_set_buf[2] << 8 | recv_set_buf[3]) == 0x10 )
  {
    memcpy( recv_uart_array_addr, recv_set_buf+4, Addr_Len );
    set_devaddr( (ADDR *)recv_uart_array_addr );
  }

}
void uart_log( char *str )
{
  uart_send( (unsigned char *)str, strlen(str), 0 );
}